function r = correlinvtstat(t, n)
% correltstat  - convert t-statistic into correlation value
%
% FORMAT:       r = correlinvtstat(t, n)
%
% Input fields:
%
%       t           t statistic
%       n           number of points in correlated series
%
% Output fields:
%
%       r           matching correlation r
%
% See also correltstat

% Version:  v0.7f
% Build:    8110521
% Date:     Nov-05 2008, 9:00 PM CET
% Author:   Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% argument check
if nargin < 2 || ...
   ~isnumeric(t) || ...
   ~isnumeric(n) || ...
    isempty(t) || ...
    isempty(n) || ...
    any(isnan(t(:)) | isinf(t(:))) || ...
    any(isinf(n(:)) | isnan(n(:)) | n(:) < 2)
    error( ...
        'BVQXtools:BadArgument', ...
        'Missing or invalid argument given.' ...
    );
end
osize = size(t);
if numel(t) == 1 && ...
    numel(n) > 1
    osize = size(t);
end
if ~isa(t, 'double')
    t = double(t(:));
else
    t = t(:);
end
if ~isa(n, 'double')
    n = double(n(:));
else
    n = n(:);
end

% formula
r = reshape(t ./ sqrt(t .* t + n - 2), osize);
